package br.com.tooist.pong2;

// treeeedeee
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

// pretty much copy paste from http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/
class Pong2Renderer implements Renderer {
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
		// Initialize our square. NOTE: is this the right place?
		GlBlock = new Pong2GlBlock();
		GlBoard = new Pong2GlBoard();
		GlHandle = new Pong2GlHandle();
	}

	private float screenWidth = 320f;
	private float screenHeight = 480f;
	// called when the screen rotates or change size somehow.
	// need to recalculate the screen proportion.
	// http://www.droidnova.com/android-3d-game-tutorial-part-vi,436.html
	// we will probably just call that once, since the game will lock in landscape mode anyway
	@Override
	public void onSurfaceChanged(GL10 gl, int w, int h) {
		screenWidth = w;
		screenHeight = h;
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, w, h);
		// black juju #http://www.droidnova.com/android-3d-game-tutorial-part-vi,436.html
		float size = .01f * (float) Math.tan(Math.toRadians(45.0) / 2);
		float ratio = w / h;
		gl.glFrustumf(-size, size, -size / ratio, size / ratio, 0.01f, 100.0f);
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);
		// Reset the projection matrix
		gl.glLoadIdentity();
		// Calculate the aspect ratio of the window
		//why? GLU.gluPerspective(gl, 45.0f, (float) w / (float) h, 0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();


		gl.glPushMatrix();
		gl.glTranslatef(0, 0, -1.00009f);
		GlBoard.draw(gl);
		gl.glPopMatrix();

		// draw the blocks
		gl.glPushMatrix();
		gl.glTranslatef(0, 0, -1.00008f);
		// Player 1
		gl.glColor4f(1f, 0.1f, 0.2f, 1.0f); // redish
		// Save the current matrix.
		gl.glPushMatrix();
		gl.glTranslatef(0, 0.8f, 0);
		GlBlock.draw(gl);
		// Restore the last matrix.
		gl.glPopMatrix();
		// Player 2
		gl.glColor4f(0f, 0.5f, 0.8f, 1.0f); // bluish
		// Save the current matrix.
		gl.glPushMatrix();
		gl.glTranslatef(0f, -0.8f, 0f);
		GlBlock.draw(gl);
		// Restore the last matrix.
		gl.glPopMatrix();
		// end blocks
		gl.glPopMatrix();


		// draw the Handles
		gl.glPushMatrix();
		gl.glTranslatef(0f, 0f, -1.00007f);
		// Player 1
		gl.glColor4f(0.9f, 0.05f, 0.1f, 1.0f); // redish
		// Save the current matrix.
		gl.glPushMatrix();
		gl.glTranslatef(0f, 1f, 0f);
		GlHandle.draw(gl);
		// Restore the last matrix.
		gl.glPopMatrix();
		// Player 2
		gl.glColor4f(0f, 0.25f, 0.9f, 1.0f); // bluish
		// Save the current matrix.
		gl.glPushMatrix();
		gl.glTranslatef(0, -1, 0);
		GlHandle.draw(gl);
		// Restore the last matrix.
		gl.glPopMatrix();
		// end blocks
		gl.glPopMatrix();
	}

	public void setColor(float r, float g, float b) {
		mRed = r;
		mGreen = g;
		mBlue = b;
	}

	private float mRed;
	private float mGreen;
	private float mBlue;
	private Pong2GlBlock GlBlock;
	private Pong2GlBoard GlBoard;
	private Pong2GlHandle GlHandle;
}
